<?php
defined('IN_IA') or exit('Access Denied');

require IA_ROOT. '/addons/lexiangpingou/core/common/defines.php';
require TG_CORE . 'class/wlloader.class.php';
wl_load()->func('global');
wl_load()->func('pdo');
wl_load()->func('tpl');
wl_load()->func('message');
wl_load()->func('print');
class lexiangpingouModuleSite extends WeModuleSite {
	
	public function __call($name, $arguments) {
		global $_W;
		$isWeb = stripos($name, 'doWeb') === 0;
		$isMobile = stripos($name, 'doMobile') === 0;
		if($isWeb || $isMobile) {
			$dir = IA_ROOT . '/addons/' . $this->modulename . '/';
			if($isWeb) {
				$dir .= 'web/';
				$controller = strtolower(substr($name, 5));
				
			}
			if($isMobile) {
				$dir .= 'app/';
				$controller = strtolower(substr($name, 8));
			}
			$file = $dir . 'index.php';
			if(file_exists($file)) {
				require $file;
				exit;
			}
		}
		trigger_error("访问的方法 {$name} 不存在.", E_USER_WARNING);
		
		return null;
	}
	
	/*支付结果返回*/
	public function payResult($params) {
		global $_W, $_GPC;
		wl_load()->model('goods');
		wl_load()->model('merchant');
		wl_load()->model('order');
		wl_load()->model('group');
		load()->model('mc');
		load() -> model('account');
		$sleeptime=rand(0.01,0.5);
		sleep($sleeptime);
		/*写入文件*/
		$pay_log = $params;$pay_log['createtime']= date("Y-m-d H:i:s",TIMESTAMP);
		//file_put_contents(TG_DATA."payresult.log", var_export($pay_log, true).PHP_EOL, FILE_APPEND);
		$success = FALSE;
		
		
		$order_out = order_get_by_params(" orderno = '{$params['tid']}'");
		
		if($order_out['status']==0){
			$data = array('status' => $params['result'] == 'success' ? 1 : 0);
			if($params['is_usecard']==1){
				$fee = $params['card_fee'];
				$data['is_usecard'] = 1;
			}else{
				$fee = $params['fee'];
			}
			$paytype = array('credit' => 1, 'wechat' => 2, 'alipay' => 2, 'delivery' => 3);
			$data['pay_type'] = $paytype[$params['type']];
			if ($params['type'] == 'wechat') {
				$data['transid'] = $params['tag']['transaction_id'];
			}
			$data['ptime'] = TIMESTAMP;
			$data['price'] = $fee;
			$data['starttime'] = TIMESTAMP;

			/*后台通知，修改状态*/
			if ($params['result'] == 'success' && $params['from'] == 'notify') {
				//订单通知店主
				if(!empty($goodsInfo['openid']))
				{
					$member=pdo_fetch('select nickname from '.tablename('tg_member').' where openid=:openid',array(':openid'=>$order_out['openid']));
					$type='预约订单';

					$notice_message='下单人：【'.$member['nickname'].'】订单金额：【 '.$order_out['pay_price'].'】';
					result_type($goodsInfo['openid'],'您有一笔'.$order_out['goodsname'].'的新订单',$type,$notice_message,'');

				}

										

							/*处理优惠券*/
							$is_usecard = $order_out['is_usecard'];
							if($is_usecard==1){
								wl_load()->model('activity');
								$coupon_id = $order_out['couponid'];
								pdo_update('tg_coupon',array('use_time'=>$params['paytime']), array('coupon_template_id' => $coupon_id));
								coupon_quantity_issue_increase($coupon_id, 1);
							}
							/*处理代付*/
							if($order_out['openid'] != $params['user']){
								pdo_update('tg_order',array('ordertype'=>1,'helpbuy_opneid'=>$params['user']), array('orderno' => $params['tid']));
								$time = date("Y-m-d H:i:s",$params['paytime']);
								$url = app_url('order/order/detail', array('id' => $order_out['id']));
								daipay_success($order_out['openid'], $fee, $order_out['othername'], $params['tid'], $goodsInfo['gname'], $time, $order_out['message'], $url);
								daipay_success($params['user'], $fee, $order_out['othername'], $params['tid'], $goodsInfo['gname'], $time, $order_out['message'], $url);
							}
							if($order_out['couponid'] > 0){
								pdo_update('tg_coupon',array('uid' => 2, 'use_time' => TIMESTAMP), array('id' => $order_out['couponid']));
							}
							/*更新订单状态*/
							pdo_update('tg_order', $data, array('orderno' => $params['tid']));
							$url = app_url('order/group', array('tuan_id' => $order_out['tuan_id']));
							if ($order_out['is_tuan'] == 0) {


											/*更改库存*/
										if($order_out['g_id']>0)
										{
											if ($goodsInfo['gnum'] >=$order_out['gnum']) {
												goods_update_by_params(array('gnum' => $goodsInfo['gnum'] - $order_out['gnum'], 'salenum' => $goodsInfo['salenum'] + $order_out['gnum']), array('id' => $order_out['g_id']));
											}elseif(!empty($goodsInfo['gnum'])){
												goods_update_by_params(array('gnum' => $goodsInfo['gnum'] - $order_out['gnum'], 'salenum' => $goodsInfo['salenum'] + $order_out['gnum']), array('id' => $order_out['g_id']));
											}
											/*增加历史购买数量*/
										$old_data=pdo_fetch("select * from ".tablename('tg_goods_openid').' where uniacid=:uniacid and openid=:openid and g_id=:g_id',array(':uniacid'=>$order_out['uniacid'],':openid'=>$order_out['openid'],':g_id'=>$order_out['g_id']));
										if(empty($old_data))
										{
											$logdata=array(
												'g_id'=>$order_out['g_id'],
												'openid'=>$order_out['openid'],
												'num'=>$order_out['gnum'],
												'uniacid'=>$order_out['uniacid']
												);
												pdo_insert('tg_goods_openid', $logdata);
										}else{
											pdo_update('tg_goods_openid', array('num' => $old_data['num'] +$order_out['gnum']), array('id' => $old_data['id']));
												
										}
										
										/*增加历史购买数量*/
											/*更改规格库存*/
											if(!empty($order_out['optionname']))
											{
												$stock=pdo_fetch("select * from ". tablename('tg_goods_option')." where goodsid=:goodsid and title=:title",array(':goodsid'=>$order_out['g_id'],':title'=>$order_out['optionname']));
												pdo_update('tg_goods_option', array('stock' => $stock['stock'] - $order_out['gnum']), array('goodsid' => $order_out['g_id'],'title'=>$order_out['optionname']));
											}
										}
										else{
											/*判定技师该时间段是否被占用*/
											$jishi_status=0;
											$sende="select  * from " . tablename('tg_collect') . " where   orderno= '".$order_out['orderno']."'  ";
											$sendelist = pdo_fetchall($sende);
											foreach($sendelist as $key=> $value){
												$old_order=pdo_fetch("SELECT a.starttime as start,a.endtime as end FROM `ims_tg_collect` as a ,ims_tg_order as b where a.orderno=b.orderno and b.status=8 and a.orderno<>'{$value['orderno']}' and a.sid={$value['sid']} and a.jishi_id='{$value['jishi_id']}' and a.starttime = {$value['starttime']}");
												if(!empty($old_order))
												{
													$jishi_status=1;
													break;
												}else{
													$goodsInfos=pdo_fetch("select id,gnum,salenum from". tablename('tg_goods')." where id=".$value['sid']);
													pdo_update('tg_goods', array('gnum' => $goodsInfos['gnum'] - $value['num'], 'salenum' => $goodsInfos['salenum'] + $value['num']), array('id' => $value['sid']));

													/*增加历史购买数量*/
													$old_data=pdo_fetch("select * from ".tablename('tg_goods_openid').' where uniacid=:uniacid and openid=:openid and g_id=:g_id',array(':uniacid'=>$value['uniacid'],':openid'=>$value['openid'],':g_id'=>$value['sid']));
													if(empty($old_data))
													{
														$logdata=array(
																'g_id'=>$value['sid'],
																'openid'=>$value['openid'],
																'num'=>$value['num'],
																'uniacid'=>$value['uniacid']
														);
														pdo_insert('tg_goods_openid', $logdata);
													}else{
														pdo_update('tg_goods_openid', array('num' => $old_data['num'] +$value['num']), array('id' => $old_data['id']));

													}

													/*增加历史购买数量*/

													/*更改规格库存*/
													if(!empty($value['item']))
													{
														$stocks=pdo_fetch("select * from ". tablename('tg_goods_option')." where goodsid=:goodsid and title=:title",array(':goodsid'=>$value['sid'],':title'=>$value['item']));
														pdo_update('tg_goods_option', array('stock' => $stocks['stock'] - $value['num']), array('goodsid' => $value['sid'],'title'=>$value['item']));
													}
												}

											}
											if($jishi_status==1){
												if($_W['uniacid']!=6&&$_W['uniacid']!=4){
													pdo_update('tg_order', array('status' => 10), array('orderno' => $params['tid']));//2->8
												}else{
													pdo_update('tg_order', array('status' => 8), array('orderno' => $params['tid']));//2->8
												}


											}else{
												tuan_print($order_out['id']);
												$url = app_url('order/order/detail', array('id' => $order_out['id']));
												if($order_out['dispatchtype']==3)
												{
													$url = app_url('order/order/detail', array('id' => $order_out['id'],'b'=>1));
												}
												pdo_update('tg_order', array('status' => 8), array('orderno' => $params['tid']));//2->8
												pay_onesuccess($order_out['orderno'],$order_out['openid'], $fee, TIMESTAMP, $url);
											}

										}
									}
						}

				
		}
				/*前台通知*/
		if ($params['result'] == 'success' && $params['from'] == 'return') {

				echo "<script>  location.href='" . app_url('pay/success',array('orderno'=>$order_out['orderno'])) . "';</script>";
				exit ;

		}
 }
}
